package com.wyh.common.utils;

import com.wyh.common.exception.ResourceNotFoundException;

import static com.wyh.common.enums.ResultCodeEnum.SYSTEM_ERROR;

public class SubStringFourUtils {
    public static String subStringAfterNthSlashFromEnd(String str, int index, char splitStr) {
        if (str == null || str.isEmpty() || index <= 0) {
            return "";
        }
        int slashCounts = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == splitStr) {
                slashCounts++;
            }
        }

        if (index > slashCounts) {
            throw new ResourceNotFoundException(SYSTEM_ERROR.getCode(),"资源名称不合法");
        }
        int slashCount = 0;
        int lastIndex = str.length() - 1;
        for (int i = lastIndex; i >= 0; i--) {
            if (str.charAt(i) == splitStr) {
                slashCount++;
                if (slashCount == index) {
                    return str.substring(i + 1);
                }
            }
        }
        return "";
    }

}

